//
//  DESUtil.m
//  ScholarsBridge
//
//  Created by 李亚琪 on 2016/11/30.
//  Copyright © 2016年 mingthink. All rights reserved.
//

#import "DESUtil.h"
#import "GTMBase64.h"
#include <CommonCrypto/CommonCryptor.h>

@implementation DESUtil

+(NSString*) decryptUseDES:(NSString*)cipherText key:(NSString*)key {
    // 利用 GTMBase64 解碼 Base64 字串
    NSData* cipherData = [GTMBase64 decodeString:cipherText];
    unsigned char buffer[1024];
    memset(buffer, 0, sizeof(char));
    size_t numBytesDecrypted = 0;

    // IV 偏移量不需使用
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
                                          kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding | kCCOptionECBMode,
                                          [key UTF8String],
                                          kCCKeySizeDES,
                                          nil,
                                          [cipherData bytes],
                                          [cipherData length],
                                          buffer,
                                          1024,
                                          &numBytesDecrypted);
    NSString* plainText = nil;
    if (cryptStatus == kCCSuccess) {
        NSData* data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesDecrypted];
        plainText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    }
    return plainText;}

+(NSString *) encryptUseDES:(NSString *)clearText key:(NSString*)key
{
    NSMutableData *data = [clearText dataUsingEncoding:NSUTF8StringEncoding].mutableCopy;
    unsigned char buffer[1024];
    memset(buffer, 0, sizeof(char));
    size_t numBytesEncrypted = 0;
    
    //Byte iv[] = {1,2,3,4,5,6,7,8};
    //kCCOptionPKCS7Padding和kCCOptionECBMode。如果我们第三个变量写成kCCOptionPKCS7Padding|kCCOptionECBMode，就表示运用了ECB加密模式，并且使用PKCS7Padding的填充模式进行加密。所以单单使用kCCOptionPKCS7Padding就代表了CBC加密模式
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
                                          kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding | kCCOptionECBMode,
                                          [key UTF8String],
                                          kCCKeySizeDES,
                                          nil,
                                          [data bytes],
                                          [data length],
                                          buffer,
                                          1024,
                                          &numBytesEncrypted);

    NSString* plainText = nil;
    if (cryptStatus == kCCSuccess) {
        NSData *dataTemp = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
        //将加密后的数据用Base64转换
        plainText = [[NSString alloc] initWithData:[GTMBase64 encodeData:dataTemp] encoding:NSUTF8StringEncoding];
    }else{
        NSLog(@"DES加密失败");
    }
    return plainText;
}

@end
